<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>中文编码测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .test-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .test-section h3 {
            margin-top: 0;
            color: #333;
        }
        .result {
            background-color: #f8f9fa;
            padding: 10px;
            border-radius: 4px;
            margin: 10px 0;
            font-family: monospace;
            white-space: pre-wrap;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .loading {
            color: #666;
            font-style: italic;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>多智能体会议系统 - 中文编码测试</h1>
        <p>此页面用于测试API响应中的中文字符编码问题修复效果。</p>
        
        <div class="test-section">
            <h3>1. 健康检查测试</h3>
            <button onclick="testHealthCheck()">测试健康检查</button>
            <div id="health-result" class="result"></div>
        </div>
        
        <div class="test-section">
            <h3>2. 智能体发言测试</h3>
            <button onclick="testAgentSpeak()">测试智能体发言 (Agent ID: 2)</button>
            <div id="agent-result" class="result"></div>
        </div>
        
        <div class="test-section">
            <h3>3. Unicode编码检测</h3>
            <button onclick="testUnicodeDetection()">检测Unicode编码</button>
            <div id="unicode-result" class="result"></div>
        </div>
        
        <div class="test-section">
            <h3>4. 原始响应对比</h3>
            <button onclick="testOriginalResponse()">测试原始响应格式</button>
            <div id="original-result" class="result"></div>
        </div>
    </div>

    <script>
        const API_BASE = 'http://111.229.108.199:5000';
        
        // 检测字符串中是否包含Unicode转义序列
        function hasUnicodeEscapes(str) {
            return /\\u[0-9a-fA-F]{4}/.test(str);
        }
        
        // 解码Unicode转义序列
        function decodeUnicodeEscapes(str) {
            return str.replace(/\\u([0-9a-fA-F]{4})/g, (match, code) => {
                return String.fromCharCode(parseInt(code, 16));
            });
        }
        
        // 显示结果
        function showResult(elementId, data, isSuccess = true) {
            const element = document.getElementById(elementId);
            element.className = `result ${isSuccess ? 'success' : 'error'}`;
            element.textContent = JSON.stringify(data, null, 2);
        }
        
        // 显示加载状态
        function showLoading(elementId) {
            const element = document.getElementById(elementId);
            element.className = 'result loading';
            element.textContent = '正在测试...';
        }
        
        // 测试健康检查
        async function testHealthCheck() {
            showLoading('health-result');
            try {
                const response = await fetch(`${API_BASE}/api/health`);
                const data = await response.json();
                
                const result = {
                    status: 'success',
                    hasUnicodeEscapes: hasUnicodeEscapes(JSON.stringify(data)),
                    data: data
                };
                
                showResult('health-result', result, true);
            } catch (error) {
                showResult('health-result', {
                    status: 'error',
                    error: error.message
                }, false);
            }
        }
        
        // 测试智能体发言
        async function testAgentSpeak() {
            showLoading('agent-result');
            try {
                const response = await fetch(`${API_BASE}/api/agent_speak/2`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });
                const data = await response.json();
                
                // 检查content字段是否包含Unicode转义序列
                const contentStr = JSON.stringify(data.message?.content || '');
                const hasEscapes = hasUnicodeEscapes(contentStr);
                
                const result = {
                    status: 'success',
                    hasUnicodeEscapes: hasEscapes,
                    content: data.message?.content || '',
                    decodedContent: hasEscapes ? decodeUnicodeEscapes(data.message?.content || '') : data.message?.content,
                    fullResponse: data
                };
                
                showResult('agent-result', result, !hasEscapes);
            } catch (error) {
                showResult('agent-result', {
                    status: 'error',
                    error: error.message
                }, false);
            }
        }
        
        // 测试Unicode编码检测
        async function testUnicodeDetection() {
            showLoading('unicode-result');
            
            const testStrings = [
                '建议加强数据安全体系建设',
                '\u5efa\u8bae\u52a0\u5f3a\u6570\u636e\u5b89\u5168\u4f53\u7cfb\u5efa\u8bbe',
                '混合内容：建议\u52a0\u5f3a数据安全体系建设'
            ];
            
            const results = testStrings.map(str => ({
                original: str,
                hasUnicodeEscapes: hasUnicodeEscapes(str),
                decoded: hasUnicodeEscapes(str) ? decodeUnicodeEscapes(str) : str
            }));
            
            showResult('unicode-result', {
                testResults: results,
                summary: `测试了${results.length}个字符串，${results.filter(r => r.hasUnicodeEscapes).length}个包含Unicode转义序列`
            }, true);
        }
        
        // 测试原始响应格式
        async function testOriginalResponse() {
            showLoading('original-result');
            try {
                const response = await fetch(`${API_BASE}/api/agent_speak/2`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });
                
                // 获取原始响应文本
                const responseText = await response.text();
                
                const result = {
                    responseHeaders: Object.fromEntries(response.headers.entries()),
                    contentType: response.headers.get('content-type'),
                    responseText: responseText,
                    hasUnicodeEscapes: hasUnicodeEscapes(responseText),
                    isJson: response.headers.get('content-type')?.includes('application/json')
                };
                
                showResult('original-result', result, !result.hasUnicodeEscapes);
            } catch (error) {
                showResult('original-result', {
                    status: 'error',
                    error: error.message
                }, false);
            }
        }
        
        // 页面加载时自动运行测试
        window.onload = function() {
            console.log('页面加载完成，可以开始测试中文编码修复效果');
        };
    </script>
</body>
</html>
